package com.hb.trans.mvp.presenter

import android.content.DialogInterface
import com.basiclib.base.BasePresenter
import com.basiclib.lifecycle.RxLifecycleUtils
import com.basiclib.network.RetryWithDelay
import com.basiclib.network.RxErrorHandler
import com.basiclib.widget.OnDialogDismissListener
import com.hb.trans.bean.Base
import com.hb.trans.bean.Save
import com.hb.trans.global.ErrorHandleSubscriber
import com.hb.trans.mvp.contract.SignContract
import com.hb.trans.mvp.model.SignModel
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import javax.inject.Inject

class SignPresenter @Inject constructor(model: SignModel, var errorHandler: RxErrorHandler) :
    BasePresenter<SignContract.View, SignContract.Model>(model), OnDialogDismissListener {


    fun save(
        card_no: String,
        phone: String,
        user_name: String,
        car_type: String,
        inscode: String
    ) {
        mModel.save(card_no, phone, user_name, car_type, inscode)
            .subscribeOn(Schedulers.io())
            .retryWhen(RetryWithDelay())
            .doOnSubscribe { mRootView?.showLoading("请稍候...", listener = this@SignPresenter) }
            .observeOn(AndroidSchedulers.mainThread())
            .compose(RxLifecycleUtils.bindToLifecycle(mRootView!!))
            .subscribe(object : ErrorHandleSubscriber<Base<Save>>(errorHandler) {
                override fun next(re: Base<Save>) {
                    if (re.code == 1) {
                        if (re.info.equals("report")) {
                            mRootView?.saveError()
                        }
                        re.data?.let {
                            mRootView?.saveSucess()
                        }
                    } else {
                        mRootView?.showShortToast(re.message ?: "")
                    }
                }
            })
    }

    override fun onDismiss(dialog: DialogInterface?, status: Int, tag: Int) {

    }
}